{include file="public/layout" /}
<body class="bodystyle eyou_body_xin" style="cursor: default; -moz-user-select: inherit;min-width:auto;">
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
{include file="shop_product/left" /}
<div class="page min-hg-c-10" style="min-width: auto; margin-left: 100px;">
    <div class="flexigrid flexigrid-xin mb10" style="position: unset !important;overflow: unset;">
        <div class="mDiv">
            <div class="ftitle">
               {eq name="$Think.const.CONTROLLER_NAME.'@add'|is_check_access" value="1"}
               <div class="fbutton" style="float: none;">
                   <a href="{:url('ShopProduct/add', ['source'=>'add'])}"><div class="add"><span><i class="layui-icon layui-icon-addition"></i>发布商品</span></div></a>
               </div>
               {/eq}
            </div>
        </div>

        <div class="fixed-bar">
            <div class="item-title">
                <ul class="tab-base nc-row">
                    <li> <a href='{:url("ShopProduct/index", ["query"=>1])}' {if condition='"index" == $Request.param.a && 1 == $param.query'} class="current" {/if}>出售中</a> </li>
                    <li> <a href='{:url("ShopProduct/index", ["query"=>2])}' {if condition='"index" == $Request.param.a && 2 == $param.query'} class="current" {/if}>仓库中</a> </li>
                    <li> <a href='{:url("ShopProduct/index", ["query"=>3])}' {if condition='"index" == $Request.param.a && 3 == $param.query'} class="current" {/if}>库存预警</a> </li>
                    <li> <a href='{:url("ShopProduct/index", ["query"=>4])}' {if condition='"index" == $Request.param.a && 4 == $param.query'} class="current" {/if}>回收站</a> </li>
                </ul>
            </div>
        </div>

        <div class="header-bar header-bj">
            <div class="more">
                <div class="search">
                    <div class="content" >
                        <form id="searchForm" action="{:url('ShopProduct/index')}" method="get" onsubmit="layer_loading('正在处理');" style="display: unset;">
                            {$searchform.hidden|default=''}
                            <input type="hidden" id="query" name="query" value="{$param.query}">
                            <div class="ey-form-item"><label class="users-form-item-label">商品名称：</label>
                                <div class="users-form-item-content">
                                    <div class="users-select">
                                        <div tabindex="0" class="users-select-selection">
                                            <div class="sDiv2">
                                                <input type="text" size="50" name="title" id="title" value="{$param.title}" class="qsbox" style="width: 170px;" placeholder="输入商品名称搜索...">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
    
                            <!-- <div class="ey-form-item " style="position: relative !important;">
                                <label class="users-form-item-label">商品分类：</label>
                                <div class="users-form-item-content" id="goodsStypeid">
                                    <div class="users-select">
                                        <div class="users-select-selection">
                                            <div class="sDiv2 ">
                                                <input type="text" class="qsbox fenlei" placeholder="{$stypeidCount|default='全部'}" lay-key="1" style="width: 210px;" id="stypeidCount">
                                                <i class=" layui-icon-down layui-icon" style="position: absolute;top: 9px;right: 10px;color: #444343; font-size: 10px;font-weight: bold;"></i>
                                            </div>
                                        </div>
                                        <div id="ey_fenleitanchuang" style="display: none;" onselectstart="return false;">
                                            <table class="flex-table autoht" cellpadding="0" cellspacing="0" border="0" id="arctype_table" style="width: 100%">
                                                <tbody id="treet1">
                                                {foreach name="arctype_list" item="vo" key="k" }
                                                    <tr nctype="0" {if condition="!in_array($vo.parent_id,$tree.cookied_treeclicked) && 0 < $vo.parent_id "} style="display:none;"{/if} class="parent_id_{$vo['parent_id']}" data-level="{$vo['level']}" data-id="{$vo['id']}">
                                                        <td class="typename" style="width: 100%">
                                                            <div style="margin: 10px 0;">
                                                                {eq name='vo.level' value='1'}<span class="" style="margin-left: 20px;"></span>{/eq}
                                                                {egt name='vo.level' value='2'}<span class="" style="margin-left: 40px;"></span>{/egt}
                                                                {gt name="vo.has_children" value="0"}
                                                                <img src="{in name='$vo.id' value='$tree.cookied_treeclicked'}__SKIN__/images/tv-collapsable-last.gif{else /}__SKIN__/images/tv-expandable.gif{/in}" style="float: none;" fieldid="2" status="open" nc_type="flex" onclick="treeClicked(this, {$vo['id']}, 0);" class="has_children childrenImg" data-level="{$vo['level']}" data-id="{$vo['id']}">
                                                                {else /}
                                                                <img src="__SKIN__/images/tv-collapsable-last.gif" style="float: none;" fieldid="2" status="open" nc_type="flex" class="childrenImg">
                                                                {/gt}
                                                                <label><input type="checkbox" name="stypeid[]" value="{$vo.id}" id="{$vo.id}" data-level="{$vo.level}" data-topid="{$vo.topid}" data-parent_id="{$vo.parent_id}" data-typename="{$vo.typename}" data-has_children="{$vo.has_children}" onclick="selectStypeid(this);" class="stypeid {eq name='$vo.level' value='1'} topid_{$vo.topid} {/eq}">&nbsp;{$vo.typename}</label>
                                                            </div>
                                                        </td>
                                                    </tr>
                                                {/foreach}
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div> -->

                            <div class="ey-form-item"><label class="users-form-item-label">创建时间：</label>
                                <div class="users-form-item-content">
                                    <div class="users-select">
                                        <div tabindex="0" class="users-select-selection">
                                            <div class="sDiv2">
                                                <input type="text" name="add_time" id="eYLaydateTimeLinkage" data-max_date="{php}echo date('Y-m-d');{/php}" autocomplete="off"  class="qsbox" placeholder="商品创建时间" lay-key="1" style="width: 220px;" value="{$param.add_time}">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
    
                            <div class="ey-form-item"><label class="users-form-item-label">商品销量：</label>
                                <div class="users-form-item-content">
                                    <div class="users-select">
                                        <div tabindex="0" class="users-select-selection">
                                            <div class="sDiv2">
                                                <input type="text" name="sales_num1" {notempty name="$param.sales_num1"} value="{$param.sales_num1}" {/notempty} class="qsbox" placeholder="最小值" onkeyup="this.value=this.value.replace(/\D/g,'')">
                                            </div>
                                            &nbsp;至&nbsp;
                                            <div class="sDiv2">
                                                <input type="text" name="sales_num2" {notempty name="$param.sales_num2"} value="{$param.sales_num2}" {/notempty} class="qsbox" placeholder="最大值" onkeyup="this.value=this.value.replace(/\D/g,'')">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
    
                            <div class="ey-form-item"><label class="users-form-item-label">商品价格：</label>
                                <div class="users-form-item-content">
                                    <div class="users-select">
                                        <div tabindex="0" class="users-select-selection">
                                            <div class="sDiv2">
                                                <input type="text" name="users_price1" {notempty name="$param.users_price1"} value="{$param.users_price1}" {/notempty} class="qsbox" placeholder="最小值" onkeyup="this.value=this.value.replace(/\D/g,'')">
                                            </div>
                                            &nbsp;至&nbsp;
                                            <div class="sDiv2">
                                                <input type="text" name="users_price2" {notempty name="$param.users_price2"} value="{$param.users_price2}" {/notempty} class="qsbox" placeholder="最大值" onkeyup="this.value=this.value.replace(/\D/g,'')">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                        <div class="ey-form-item ml20">
                            <div class="users-form-item-content">
                                <span class="bt-xin" id="submitForm" onclick="productQuery();">查询</span>
                                <span class="bt-xin-no" onclick="productQuery('reset');">重置</span>
                                <!-- <div class="fbutton">
                                    <a href="javascript:void(0);" onclick="ProductExcelExport(this);" data-url="{:url('Order/ajax_order_excel_export', ['export_type'=>7])}" class="bt-xin-xia"><i class="iconfont e-xiazai"></i>导出</a>
                                </div> -->
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 商品列表页面 -->
    <div id="goodsListPage">{include file="shop_product/index_list" /}</div>
</div>

<script type="text/javascript">
    $(function() {
        $('input[name*=aids]').click(function() {
            if ($('input[name*=aids]').length == $('input[name*=aids]:checked').length) {
                $('.checkAll').prop('checked', 'checked');
            } else {
                $('.checkAll').prop('checked', false);
            }
        });
        $('input[type=checkbox].checkAll').click(function() {
            $('input[type=checkbox]').prop('checked', this.checked);
        });
    });

    $(document).keydown(function(event){
        if (event.keyCode ==13) {
            $('#searchForm input[type=text]').each(function(index, item){
                if ($(item).is(':focus')) {
                    $('#searchForm').submit();
                }
            });
        }
    });

    $(document).ready(function() {
        // 表格行点击选中切换
        $('#flexigrid > table>tbody >tr').click(function() {
            $(this).toggleClass('trSelected');
        });

        // 批量操作
        $(".dropdown-bt").click(function(){
            $(".dropdown-menus").slideToggle(200);
            event.stopPropagation();
        })
        $(document).click(function(){
            $(".dropdown-menus").slideUp(200);
            event.stopPropagation();
        })
        
        $(".goodsQuickEdit").mouseenter(function() {
            $(this).find('a').show();
        });
        $(".goodsQuickEdit").mouseleave(function() {
            $(this).find('a').hide();
        });
    });

    function eopnGoodsQuickEdit(obj, goodsSpec) {
        var iframe = document.createElement('iframe');
        iframe.style.cssText = 'position: absolute; visibility: hidden; width: 0; height: 0; border: none;';
        iframe.src = $(obj).data('url');
        
        iframe.onload = function() {
            var iframeWindow = iframe.contentWindow || iframe.contentDocument.defaultView;
            var pageWidth = iframeWindow.document.documentElement.scrollWidth + 0; // 获取页面宽度并加上 20
            var pageHeight = iframeWindow.document.documentElement.scrollHeight + 70; // 获取页面高度并加上 20
            var viewportHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0); // 获取屏幕可视区域的高度
            var maxHeight = Math.floor(viewportHeight * 0.8); // 计算弹窗的最大高度为屏幕可视区域高度的 80%
            var popupHeight = Math.min(pageHeight, maxHeight); // 取页面高度和最大高度的较小值作为弹窗的高度
            layer.open({
                type: 2,
                fixed: true,
                maxmin: false,
                shadeClose: false,
                shade: layer_shade,
                area: [pageWidth + 'px', popupHeight + 'px'], // 将页面宽度和弹窗高度作为弹窗的高宽
                title: '快捷编辑',
                content: $(obj).data('url')
            });
            document.body.removeChild(iframe);
        };
        document.body.appendChild(iframe);
    }

    // 商品查询搜索
    function productQuery(isReset) {
        if (isReset && 'reset' == isReset) {
            window.location.href = "{:url('ShopProduct/index', ['query'=>$Request.param.query])}";
        } else {
            $('#searchForm').submit();
        }
    }

    // 获取post提交的aid数组
    function getPostAids(aid) {
        var aids = [];
        if (!aid || aid === null) {
            $('input[name^=aids]').each(function(i, o) {
                if ($(o).is(':checked')) aids.push($(o).val());
            });
            if (aids.length == 0) return false;
        } else {
            aids = [aid];
        }

        return aids;
    }

    // 商品状态更新
    function goodsArcrank(aid, arcrank) {
        // 获取post提交的aid数组
        var aids = getPostAids(aid);
        if (false == aids) {
            showErrorAlert('请至少选择一项');
            return false;
        }

        // 执行提交
        var title = 1 === parseInt(arcrank) ? '上架' : '下架';
        showConfirm('确定' + title + '该商品？', {}, function() {
            layer_loading('正在处理');
            $.ajax({
                type: 'post',
                url : "{:url('ShopProduct/goods_arcrank')}",
                data: {aids: aids, arcrank: arcrank, _ajax: 1},
                dataType: 'json',
                success: function(res) {
                    layer.closeAll();
                    if (1 === parseInt(res.code)) {
                        showSuccessMsg(res.msg, 1500, function() {
                            window.location.reload();
                        });
                    } else {
                        showErrorAlert(res.msg);
                    }
                },
                error: function(e) {
                    layer.closeAll();
                    showErrorAlert(e.responseText);
                }
            });
        });
    }

    // 商品复制
    function goodsCopy(aid) {
        // 获取post提交的aid数组
        var aids = getPostAids(aid);
        if (false == aids) {
            showErrorAlert('请至少选择一项');
            return false;
        }

        // 执行提交
        showConfirm('确定复制该商品？', {}, function() {
            layer_loading('正在处理');
            $.ajax({
                type: 'post',
                url : "{:url('ShopProduct/goods_copy')}",
                data: {aids: aids, _ajax: 1},
                dataType: 'json',
                success: function(res) {
                    layer.closeAll();
                    if (1 === parseInt(res.code)) {
                        showSuccessMsg(res.msg, 1500, function() {
                            window.location.reload();
                        });
                    } else {
                        showErrorAlert(res.msg);
                    }
                },
                error: function(e) {
                    layer.closeAll();
                    showErrorAlert(e.responseText);
                }
            });
        });
    }

    // 商品删除(query=1，2，3回收站、 query=4彻底删除)
    function goodsDelete(aid) {
        // 获取post提交的aid数组
        var aids = getPostAids(aid);
        if (false == aids) {
            showErrorAlert('请至少选择一项');
            return false;
        }

        // 执行提交
        var query = $('#query').val();
        showConfirm(4 === parseInt(query) ? '确定彻底删除该商品？' : '确认将该商品放入回收站？', {}, function() {
            layer_loading('正在处理');
            $.ajax({
                type: 'post',
                url : "{:url('ShopProduct/goods_delete')}",
                data: {aids: aids, query: query, _ajax: 1},
                dataType: 'json',
                success: function(res) {
                    layer.closeAll();
                    if (1 === parseInt(res.code)) {
                        showSuccessMsg(res.msg, 1500, function() {
                            window.location.reload();
                        });
                    } else {
                        showErrorAlert(res.msg);
                    }
                },
                error: function(e) {
                    layer.closeAll();
                    showErrorAlert(e.responseText);
                }
            });
        });
    }

    // 商品恢复(从伪删除恢复到原先状态)
    function goodsRestore(aid) {
        // 获取post提交的aid数组
        var aids = getPostAids(aid);
        if (false == aids) {
            showErrorAlert('请至少选择一项');
            return false;
        }

        showConfirm('确定恢复该商品？', {}, function() {
            layer_loading('正在处理');
            $.ajax({
                type: 'post',
                url : "{:url('ShopProduct/goods_restore')}",
                data: {aids: aids, _ajax: 1},
                dataType: 'json',
                success: function(res) {
                    layer.closeAll();
                    if (1 === parseInt(res.code)) {
                        showSuccessMsg(res.msg, 1500, function() {
                            window.location.reload();
                        });
                    } else {
                        showErrorAlert(res.msg);
                    }
                },
                error: function(e) {
                    layer.closeAll();
                    showErrorAlert(e.responseText);
                }
            });
        });
    }
</script>

<!-- 日期联动选择js文件 start -->
{eyou:static file="__PUBLIC__/static/common/js/ey_laydate_time_linkage.js"/}
<!-- 日期联动选择js文件 end -->
{include file="public/footer" /}

<!-- 
<input type="hidden" id="post_stypeid" value="{$stypeidStr}">
<script type="text/javascript">
    $(function() {
        var stypeidStr = $('#post_stypeid').val();
        var stypeidArr = stypeidStr.split(',');
        $('.stypeid').each(function() {
            var _this = $(this);
            var index = $.inArray(String(_this.val()), stypeidArr);
            if (parseInt(index) > -1) {
                _this.attr('checked', true);
                $('#' + _this.data('topid')).attr('checked', true);
                $('#' + _this.data('parent_id')).attr('checked', true);
            }
        });

        $('#goodsStypeid').mouseover(function() {
            $('#ey_fenleitanchuang').show();
        });
        $('#goodsStypeid').mouseout(function() {
            $('#ey_fenleitanchuang').hide();
        });
    });

    function selectStypeid(obj) {
        var isSelect = $(obj).prop('checked');
        var selectID = parseInt($(obj).val());
        var selectLevel = parseInt($(obj).data('level'));
        var selectTopID = parseInt($(obj).data('topid'));
        var selectParentID = parseInt($(obj).data('parent_id'));
        var selectValue1 = selectValue2 = selectValue3 = 0;
        var postStypeidArr = [];
        $('.stypeid').each(function() {
            var _this = $(this);
            if (0 === selectLevel) {
                if (selectID === parseInt(_this.data('topid'))) _this.attr('checked', isSelect);
            } else if (1 === selectLevel) {
                if (selectID === parseInt(_this.data('parent_id'))) _this.attr('checked', isSelect);
            }
            if (1 === selectLevel && 1 === parseInt(_this.data('level')) && selectTopID === parseInt(_this.data('topid'))) {
                if (true === _this.prop('checked')) selectValue1++;
                if (0 === parseInt(selectValue1)) {
                    $('#' + selectTopID).attr('checked', false);
                } else {
                    $('#' + selectTopID).attr('checked', true);
                }
            }
            if (2 === selectLevel && 2 === parseInt(_this.data('level')) && selectParentID === parseInt(_this.data('parent_id'))) {
                if (true === _this.prop('checked')) selectValue2++;
                if (0 === parseInt(selectValue2)) {
                    $('#' + selectParentID).attr('checked', false);
                } else {
                    $('#' + selectTopID).attr('checked', true);
                    $('#' + selectParentID).attr('checked', true);
                }
            }

            // 选中的分类ID加入数组
            if (0 === _this.data('has_children') && true === _this.prop('checked')) postStypeidArr.push(_this.val());
        });

        // 处理二级分类是否选中
        $('.topid_' + selectTopID).each(function() {
            if (true === $(this).prop('checked')) selectValue3++;
        });
        if (0 === parseInt(selectValue3)) $('#' + selectTopID).attr('checked', false);

        // 所有选中的分类数量
        var selectStypeidLength = $('#arctype_table input[name="stypeid[]"]:checked').length;
        if (parseInt(selectStypeidLength) > 0) {
            $('#stypeidCount').attr('placeholder', '已选中 ' + selectStypeidLength + ' 个分类');
        } else {
            $('#stypeidCount').attr('placeholder', '全部');
        }

        // 选中的分类ID数组转字符串
        var postStypeidStr = postStypeidArr.join(',');
        $('#post_stypeid').val(postStypeidStr);
    }
</script>

<script type="text/javascript">
    $(function() {
        setCookies('stypeid-treeClicked-Arr', JSON.stringify({$tree.cookied_treeclicked_arr}) );
    });

    // 点击展开 收缩节点
    function treeClicked(obj, id, reload) {
        if (id == 'all') {
            if (1 == reload) {
                var status = getCookie('stypeid-treeClicked');
                if (!status) status = $(obj).attr('data-status');
            } else {
                var status = $(obj).attr('data-status');
            }
            if (status == 'close') {
                setCookies('stypeid-treeClicked-Arr', JSON.stringify({$tree.parent_ids}));
                setCookies('stypeid-treeClicked_All', 1);//1为全部展开,0-为非全部展开
                $('tr[class^=parent_id_]').show();
                $('.childrenImg').attr('src', '__SKIN__/images/tv-collapsable-last.gif');
                $(obj).attr('data-status', 'open').attr('title','关闭所有子分类').attr('src','__SKIN__/images/tv-collapsable-last.gif');
            } else {
                setCookies('stypeid-treeClicked-Arr', '');
                setCookies('stypeid-treeClicked_All', 0);
                $('tr[data-level=0]').find('img.has_children').attr('src', '__SKIN__/images/tv-collapsable-last.gif').trigger('click');
                $('tr[class^=parent_id_]').removeClass('trSelected');
                $(obj).attr('data-status', 'close').attr('title','展开所有子分类').attr('src','__SKIN__/images/tv-expandable.gif');
            }
            setCookies('stypeid-treeClicked', status);
            return false;
        }

        var src = $(obj).attr('src');
        if (src == '__SKIN__/images/tv-expandable.gif') {
            var arr = [];
            var str = getCookie('stypeid-treeClicked-Arr');
            if ('' == str || null == str || 'null' == str) {
                arr.push(id);
            } else {
                arr = JSON.parse(str);
                if (!arr.includes(id))arr.push(id);
            }
            arr = JSON.stringify(arr);
            setCookies('stypeid-treeClicked-Arr', arr);
            $(".parent_id_"+id).show();
            $(obj).attr('src','__SKIN__/images/tv-collapsable-last.gif');
            var status = 'close';
        } else {
            var key = -1;
            var arr = [];
            var level = '';
            var str = getCookie('stypeid-treeClicked-Arr');
            if ('' != str) {
                arr = JSON.parse(str);
                key = $.inArray(id,arr);
                if (-1 < key) arr.splice(key,1);
                level = $(obj).attr('data-level');
                $(obj).parent().parent().parent().nextAll().each(function() {
                    if ($(this).attr('data-level') > level) {
                        key = $.inArray(parseInt($(this).attr('data-id')), arr);
                        if (-1 < key) arr.splice(key,1);
                    } else {
                        return false;
                    }
                });
                arr = JSON.stringify(arr);
                setCookies('stypeid-treeClicked-Arr', arr);
                setCookies('stypeid-treeClicked_All', 0);
            }

            var status = 'open';
            $(obj).attr('src', '__SKIN__/images/tv-expandable.gif');     

            // 如果是点击减号, 遍历循环他下面的所有都关闭
            var fnd = false;
            var cur_tr = obj.parentNode.parentNode.parentNode;
            var tbl = document.getElementById("arctype_table");
            for (var i = 0; i < tbl.rows.length; i++) {
                var row = tbl.rows[i];
                if (row == cur_tr) {
                    fnd = true;
                } else {
                    if (fnd == true) {
                        var cur_level = $(cur_tr).data('level');
                        var level = parseInt($(row).data('level'));
                        if (level > cur_level) {
                            $(row).hide();        
                            $(row).find('img.has_children').attr('src','__SKIN__/images/tv-expandable.gif');
                        } else {
                            fnd = false;
                            break;
                        }
                    }
                }
            }
        }
        setCookies('stypeid-treeClicked', status);
    }

    // AJAX调用下载
    function ProductExcelExport(obj) {
        layer_loading('正在导出');
        $.ajax({
            type: 'post',
            url : $(obj).attr('data-url'),
            data: $('#searchForm').serialize(),
            dataType: 'json',
            success: function(res) {
                layer.closeAll();
                if (1 == res.code) {
                    window.location.href = res.url;
                } else {
                    showErrorAlert(res.msg);
                }
            },
            error: function(e) {
                layer.closeAll();
                showErrorAlert(e.responseText);
            }
        });
    }
</script>
-->